﻿using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDxfDocument

{
    class ISYC_301_04
    {
        //图F.6  ISYC 301-04-粗柄带颈螺纹挤压丝锥
        public static void ISYC_301_04_j(Dictionary<string, Object> data)  //粗柄挤压丝锥
        {
            DxfDocument dxf = new DxfDocument();
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer cutting = new Layer("cutting") { Color = AciColor.Blue };    //切削层

            double pi = Math.PI;

            double sl = Convert.ToDouble(data["oal"]);
            //double sl = 60;
            double scale;
            if (sl <= 50)
            {
                scale = 2.2;
            }
            else if (50 < sl && sl <= 75)
            {
                scale = 1.8;
            }
            else if (75 < sl && sl <= 100)
            {
                scale = 1.4;
            }
            else
            {
                scale = 1;
            }
            
            double oal = Convert.ToDouble(data["oal"]) * scale;
            double tcpd = Convert.ToDouble(data["tcpd"]) * scale;
            double dmm = Convert.ToDouble(data["dmm"]) * scale;
            double lu = Convert.ToDouble(data["lu"]) * scale;
            double ls = Convert.ToDouble(data["ls"]) * scale;
            double thl = Convert.ToDouble(data["thl"]) * scale;
            double tcl = Convert.ToDouble(data["tcl"]) * scale;
            double td = Convert.ToDouble(data["td"]) * scale;
            double dn = Convert.ToDouble(data["dn"]) * scale;
            double isframe = Convert.ToDouble(data["isframe"]);
            //double fha = Convert.ToDouble(data["fha"]);
            string path = Convert.ToString(data["path"]);
            /*
            double oal = 60 * scale;
            double tcpd = 11* scale;
            double dmm = 14 * scale;
            double lu = 25 * scale;
            double ls = 33 * scale;
            double thl = 22 * scale;
            double tcl = 2 * scale;
            double td = 12 * scale;
            double dn = 10* scale;
            double isframe = 0;
            //double fha = Convert.ToDouble(data["fha"]);
            string path = Convert.ToString(data["path"]);*/
            Vector2 p0 = new Vector2(0, 0);
            Vector2 p0p = new Vector2(p0.X, p0.Y - oal);
            Vector2 p1 = fun.Polar(p0, 0, dmm / 2);
            Vector2 p2 = fun.Polar(p1, 1.5 * pi, ls);
            Vector2 p3 = new Vector2(p0.X + dn / 2, p0p.Y + lu);
            Vector2 p4 = new Vector2(p3.X, p0p.Y + thl + 0.5);
            Vector2 p5 = new Vector2(p0.X + tcpd / 2, p0p.Y + thl);
            Vector2 p6 = new Vector2(p0.X + td / 2, p5.Y);
            Vector2 p7 = new Vector2(p6.X, p0p.Y + tcl);
            Vector2 p8 = fun.Polar(p0p, 0, tcpd / 2);
            Vector2 p9 = new Vector2(p0.X - p8.X, p8.Y);
            Vector2 p10 = new Vector2(p0.X - p7.X, p7.Y);
            Vector2 p11 = new Vector2(p0.X - p6.X, p6.Y);
            Vector2 p12 = new Vector2(p0.X - p5.X, p5.Y);
            Vector2 p13 = new Vector2(p0.X - p4.X, p4.Y);
            Vector2 p14 = new Vector2(p0.X - p3.X, p3.Y);
            Vector2 p15 = new Vector2(p0.X - p2.X, p2.Y);
            Vector2 p16 = new Vector2(p0.X - p1.X, p1.Y);

            Vector2 p20 = fun.Polar(p0, 0, dmm / 10);
            Vector2 p21 = fun.Polar(p20, 0, dmm * 0.3);
            Vector2 p22 = fun.Polar(p21, 1.5 * pi, oal / 10);
            Vector2 p23 = new Vector2(p20.X, p22.Y);
            Vector2 p24 = fun.Polar(p0, pi, dmm / 10);
            Vector2 p25 = fun.Polar(p24, pi, dmm * 0.3);
            Vector2 p26 = new Vector2(p25.X, p23.Y);
            Vector2 p27 = new Vector2(p24.X, p23.Y);

            p0p = fun.rotatePoint(p0p);
            p1 = fun.rotatePoint(p1);
            p2 = fun.rotatePoint(p2);
            p3 = fun.rotatePoint(p3);
            p4 = fun.rotatePoint(p4);
            p5 = fun.rotatePoint(p5);
            p6 = fun.rotatePoint(p6);
            p7 = fun.rotatePoint(p7);
            p8 = fun.rotatePoint(p8);
            p9 = fun.rotatePoint(p9);
            p10 = fun.rotatePoint(p10);
            p11 = fun.rotatePoint(p11);
            p12 = fun.rotatePoint(p12);
            p13 = fun.rotatePoint(p13);
            p14 = fun.rotatePoint(p14);
            p15 = fun.rotatePoint(p15);
            p16 = fun.rotatePoint(p16);

            p20 = fun.rotatePoint(p20);
            p21 = fun.rotatePoint(p21);
            p22 = fun.rotatePoint(p22);
            p23 = fun.rotatePoint(p23);
            p24 = fun.rotatePoint(p24);
            p25 = fun.rotatePoint(p25);
            p26 = fun.rotatePoint(p26);
            p27 = fun.rotatePoint(p27);

            LwPolyline poly1 = new LwPolyline();
            poly1.Vertexes.Add(new LwPolylineVertex(p1));
            poly1.Vertexes.Add(new LwPolylineVertex(p2));
            poly1.Vertexes.Add(new LwPolylineVertex(p3));
            poly1.Vertexes.Add(new LwPolylineVertex(p4));
            poly1.Vertexes.Add(new LwPolylineVertex(p5));
            poly1.Vertexes.Add(new LwPolylineVertex(p6));
            poly1.Vertexes.Add(new LwPolylineVertex(p7));
            poly1.Vertexes.Add(new LwPolylineVertex(p8));
            poly1.Vertexes.Add(new LwPolylineVertex(p9));
            poly1.Vertexes.Add(new LwPolylineVertex(p10));
            poly1.Vertexes.Add(new LwPolylineVertex(p11));
            poly1.Vertexes.Add(new LwPolylineVertex(p12));
            poly1.Vertexes.Add(new LwPolylineVertex(p13));
            poly1.Vertexes.Add(new LwPolylineVertex(p14));
            poly1.Vertexes.Add(new LwPolylineVertex(p15));
            poly1.Vertexes.Add(new LwPolylineVertex(p16));
            poly1.Vertexes.Add(new LwPolylineVertex(p1));
            fun.PolyFill(dxf, poly1, 0.3, contour);

            Line dotline1 = new Line(fun.Polar(p0, 0, 2), fun.Polar(p0p, pi, 2));
            dotline1.Linetype = Linetype.Center;
            dotline1.Layer = centre;
            dxf.AddEntity(dotline1);

            Vector2[] line3 = { p25, p26, p27, p24, p26 };
            fun.drawPline(dxf, line3, 0.15, contour);
            Vector2[] line4 = { p25, p27 };
            fun.drawPline(dxf, line4, 0.15, contour);
            Vector2[] line5 = { p21, p22, p23, p20, p22 };
            fun.drawPline(dxf, line5, 0.15, contour);
            Vector2[] line6 = { p21, p23 };
            fun.drawPline(dxf, line6, 0.15, contour);

            Vector2[] line7 = { p2, p15 };
            fun.drawPlineOfw(dxf, line7, 0.3, contour);
            Vector2[] line8 = { p3, p14 };
            fun.drawPlineOfw(dxf, line8, 0.3, contour);
            Vector2[] line9 = { p4, p13 };
            fun.drawPlineOfw(dxf, line9, 0.3, contour);
            Vector2[] line10 = { p5, p12 };
            fun.drawPlineOfw(dxf, line10, 0.3, contour);

            Vector2[] xiline1 = { p5, p8 };
            fun.drawPline(dxf, xiline1, 0.15, contour);
            Vector2[] xiline2 = { p9, p12 };
            fun.drawPline(dxf, xiline2, 0.15, contour);

            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(p6));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(p7));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(p8));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(p9));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(p10));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(p11));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsubofline(dxf, htype1, hnumst1, 85, 20, hline1, cutting);

            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 1.5;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 2.5;
            double offset = 20;

            LinearDimension linedim1 = new LinearDimension(new Line(p16, p9), offset * 3, 0, dimensionStyle);
            linedim1.UserText = String.Format("{0}","OAL") ;
            dxf.AddEntity(linedim1);

            LinearDimension linedim2 = new LinearDimension(new Line(p15, p16), offset, 0, dimensionStyle);
            linedim2.UserText = String.Format("{0}", "LS");
            dxf.AddEntity(linedim2);

            LinearDimension linedim3 = new LinearDimension(new Line(p9, p14), offset * 2, 0, dimensionStyle);
            linedim3.UserText = String.Format("{0}", "LU");
            dxf.AddEntity(linedim3);

            LinearDimension linedim4 = new LinearDimension(new Line(p9, p11), offset, 0, dimensionStyle);
            linedim4.UserText = String.Format("{0}","THL");
            dxf.AddEntity(linedim4);

            LinearDimension linedim5 = new LinearDimension(new Line(p1, p16), offset, 270, dimensionStyle);
            linedim5.UserText = String.Format("{0}", "DCONMS");
            dxf.AddEntity(linedim5);

            LinearDimension linedim6 = new LinearDimension(new Line(p7, p8), offset, 180, dimensionStyle);
            linedim6.UserText = String.Format("{0}", "TCL");
            dxf.AddEntity(linedim6);

            LinearDimension linedim7 = new LinearDimension(new Line(p8, p9), offset, 90, dimensionStyle);
            linedim7.UserText = String.Format("{0}", "TCPD");
            dxf.AddEntity(linedim7);

            LinearDimension linedim8 = new LinearDimension(new Line(p7, p10), offset * 2, 90, dimensionStyle);
            linedim8.UserText = String.Format("{0}", "TD");
            dxf.AddEntity(linedim8);

            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 210 - oal / 2, p0.Y + 148.5), data);
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }


    }
}


